import App from './App'
// 导入store
import store from '@/store/store.js'
//导入网络请求
import { $http } from '@escook/request-miniprogram'
//挂载在uni上

$http.baseUrl = "https://api-hmugo-web.itheima.net"
$http._post = $http.post
$http.post = function(url,data){
  return new Promise((reslove,reject)=>{
    if(url==='/api/public/v1/users/wxlogin'){
      setTimeout(()=>{
        reslove({
          data: {
            message: {
              token: 'abc'
            },
            meta: {
              status: 200
            }
          }
        })
      }, 500)
    }else if(url==='/api/public/v1/my/orders/create'){
       setTimeout(()=>{
         reslove({
           data: {"message":{"order_id":3508,"user_id":56,"order_number":"HMDD20210830000000003508","order_price":0.01,"order_pay":"0","is_send":"否","trade_no":"","order_fapiao_title":"个人","order_fapiao_company":"","order_fapiao_content":"","consignee_addr":"广东省广州市海珠区新港中路397号","pay_status":"0","create_time":1630287357,"update_time":1630287357,"order_detail":null,"goods":[{"id":6557,"order_id":3508,"goods_id":43986,"goods_price":13999,"goods_number":2,"goods_total_price":27998}]},"meta":{"msg":"创建订单成功","status":200}}
         })
       }, 500)     
    }else if(url === '/api/public/v1/my/orders/req_unifiedorder'){
       setTimeout(()=>{
         reslove({
           data: {"message":{"pay":{"timeStamp":"1630288705","nonceStr":"LbyHMGj5nFHOXOyL","package":"prepay_id=wx30095823419243678b2090da9b30a40000","signType":"MD5","paySign":"7EE5FC669AB7FDE73EC8EF7E3A3BE693"},"order_number":"HMDD20210830000000003509"},"meta":{"msg":"预付订单生成成功","status":200}}
         })
       }, 500)    
    }else{
      $http._post (url, data).then((data)=>{
        reslove(data)
      }).catch((err)=>{
        reject(err)
      })
    }
  })
}
uni.$http = $http
//请求拦截器
$http.beforeRquest = function(options){
	uni.showLoading({
		title:"数据加载中..."
	})
	//判断是否为有权限的接口
	if(options.url.indexOf('/my/') != -1){
		options.header ={
			Authorization:store.state.m_user.token
		}
	}
}
uni.$showMsg = function(title='数据请求失败',druation=1500){
	uni.showToast({
		title:title,
		duration:druation,
		icon:"none"
	})
}

//响应拦截器
$http.afterRequest = function(options){
	uni.hideLoading()
}
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
    ...App,
	store
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif